<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Helper\Build\Build as Builds;
use Carbon\Carbon;
use App\Models\News\News;
class Build extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'build:news {day}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成新闻';
	
	protected $limit = '500';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
		$day = (int)$this->argument('day');
		if(!$day)$day = 1;
		$Builds = new Builds();
		$date = Carbon::now()->subDay($day);
		$total = News::where('created_at', '>', $date)->count();
		if($total > $this->limit)
		{
			$page = ceil($total/$this->limit);
			for($i=1;$i<$page;$i++)
			{
				$list = News::where('created_at', '>', $date)->orderBy('id', 'asc')->offset(($i-1)*$this->limit)->limit($this->limit)->get();
				foreach($list as $row)
				{
					$arr = $Builds->news($row);
					echo $arr['msg'].chr(10);
				}
				
				sleep(1);
			}
		}else{
			$list = News::where('created_at', '>', $date)->orderBy('id', 'asc')->get();
			foreach($list as $row)
			{
				$arr = $Builds->news($row);
				echo $arr['msg'].chr(10);
			}
		}
    }
}
